    <%--
  Created by IntelliJ IDEA.
  User: djw
  Date: 2020/8/10
  Time: 15:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
    <title>教师管理</title>
    <link rel="stylesheet" href="<%=basePath%>static/css/admin.css">

    <link rel="stylesheet" href="<%=basePath%>static/css/pintuer.css">
    <link rel="stylesheet" href="<%=basePath%>static/css/bootstrap-theme.min.css">
    <link rel="stylesheet" href="<%=basePath%>static/css/bootstrap.min.css">
    <script type="text/javascript" src="<%=basePath%>static/js/jquery-1.11.3.min.js"></script>
    <script type="text/javascript" src="<%=basePath%>static/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="<%=basePath%>static/js/jqPaginator.js"></script>

</head>
<body>
<div class="panel admin-panel">
    <div class="panel-head"><strong class="icon-reorder">教师列表</strong></div>
    <div class="padding border-bottom">
        <button type="button" class="button border-yellow" onclick="addTeacherWindow() "><span class="icon-plus-square-o"></span>增加教师</button>
    </div>
    <table class="table table-hover text-center">
        <tr>

            <th style="text-align:center">教师编号</th>
            <th style="text-align:center">教师姓名</th>
            <th style="text-align:center">教师密码</th>
            <th style="text-align:center">教师性别</th>
            <th style="text-align:center">住宅地址</th>
            <th style="text-align:center">教师年龄</th>
            <th style="text-align:center">操作方式</th>
        </tr>

        <tr><tbody id="tb"></tbody></tr>

        <div id="windowUpd" style="position: absolute;top: 20%;left: 40%;width: 450px;height: 460px;border: 1px solid red;display: none;background-color: wheat">
            <center><h4>修改教师信息</h4></center>
            <hr>
            <ul>
                <br>
                <li>教师编号：<input  type="text" name="tno" id="t_noUpd"></li><br>
                <li>教师姓名：<input type="text" name="tname" id="t_nameUpd"></li><br>
                <li>教师密码：<input type="text" name="tpwd" id="t_pwdUpd"></li><br>
                <li>教师性别：<input type="text" name="sex" id="sexUpd"></li><br>
                <li>教师地址：<input type="text" name="address" id="addressUpd"></li><br>
                <li>教师年龄：<input type="text" name="age" id="ageUpd"></li><br>

                <input type="button" value="修改" onclick="updTeacher()">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <input type="button" value="取消" onclick="closeTeacherWindow()">
            </ul>
        </div>

        <div id="windowAdd" style="position: absolute;top: 20%;left: 40%;width: 450px;height: 460px;border: 1px solid red;display: none;background-color: wheat">
            <center><h4>增加教师信息</h4></center>
            <hr>
            <ul>
                <br>
                <li>教师编号：<input  type="text" name="tno" id="t_noadd"></li><br>
                <li>教师姓名：<input type="text" name="tname" id="t_nameadd"></li><br>
                <li>教师密码：<input type="text" name="tpwd" id="t_pwdadd"></li><br>
                <li>教师性别：<input type="text" name="sex" id="sexadd"></li><br>
                <li>教师地址：<input type="text" name="address" id="addressadd"></li><br>
                <li>教师年龄：<input type="text" name="age" id="ageadd"></li><br>
                <input type="button" value="增加" onclick="addTeacher()">
                <input type="button" value="取消" onclick="closeUpdWindow()">
            </ul>
        </div>

    </table>
</div>
</body>
<script type="text/javascript">
    var total = 0;//总条数
    var totalPages = 0;//总页数
    var visiblePages = 3;//显示的页码数
    var currentPage = 0;//当前页码

    $(document).ready(function () {
        //第一次访问，初始化全局变量
        showPage(-1);
        $('#pagination1').jqPaginator({
            totalPages: totalPages,
            visiblePages: visiblePages,
            currentPage: currentPage,
            totalCounts: total,
            first: '<li class="first"><a href="javascript:void(0);">第一页</a></li>',
            prev: '<li class="prev"><a href="javascript:void(0);">前一页</a></li>',
            next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
            last: '<li class="last"><a href="javascript:void(0);">最后一页</a></li>',
            page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
            onPageChange: function (num) {
                /* num代表改变过后的页码 */
                showPage(num);
                console.log(num+"+++++++++++++++++++++++")
            }

        });
    });

   /* function select() {
        /!* 第一次访问，初始化全局变量 *!/
        showPage(-1);
        $('#pagination1').jqPaginator({
            totalPages: totalPages,
            visiblePages: visiblePages,
            currentPage: currentPage,
            totalCounts: total,
            first: '<li class="first"><a href="javascript:void(0);">第一页</a></li>',
            prev: '<li class="prev"><a href="javascript:void(0);">前一页</a></li>',
            next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
            last: '<li class="last"><a href="javascript:void(0);">最后一页</a></li>',
            page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
            onPageChange: function (num) {
                // num代表改变过后的页码
                showPage(num);
            }
        });
    }*/
    function showPage(n) {
        $.ajax({
            url:"<%=basePath%>admin/queryTeacher",
            async:false,//同步请求（默认）
            type:"POST",//提交方式
            data:{"pageNum": n, "pageSize": 4},//数据，多个以键值对的形式存在
            dataType:"json",//数据格式
            success:function (data) {
                //打印data到控制台，看看有没有从后台接收到数据
                console.log(data);
                console.log(data.list);
                if (n == -1) {/*理解为第一次访问 ，初始化全局变量 */
                    total = data.total;
                    totalPages = data.pages;
                    currentPage = data.pageNum;
                }
                $("#tb").html("");
                if (data.list.length>0){
                    //使用jQuery形式遍历
                    $.each(data.list,function (n,val) {
                        var str = "";
                        str+="<tr>"+
                            "<td>"+val.tno+"</td>"+
                            "<td>"+val.tname+"</td>"+
                            "<td>"+val.tpwd+"</td>"+
                            "<td>"+val.sex+"</td>"+
                            "<td>"+val.address+"</td>" +
                            "<td>"+val.age+"</td>" +
                            "<td colspan='2'>" +
                            "<a class='button border-main' role='button' onclick='updTeacherWindow(this)'><span class='icon-edit'></span> 修改</a>" +
                            "<a class='button border-red' role='button' onclick='delTeacher(this)'><span class='icon-trash-o'></span> 删除</a></a>" +
                            "</td>"+
                            "</tr>"+
                            "<td>";
                        $("#tb").append(str);
                    })
                }else {
                    $("#tb").append("<tr><td colspan=8 align=\"center\">暂时没有可以选择的课程哦，请联系辅导员查询课程信息</td></tr>");
                }
            },
            error:function () {
                alert("服务器又又又出错啦")
            }
        });
    }

    //打开增加页面
    function addTeacherWindow() {
        $("#windowAdd").show()
    }

    //关闭增加页面
    function closeTeacherWindow() {
        $("#windowAdd").hide();
    }

    //增加教师
    function addTeacher() {
        var tno = $("#t_noadd").val();
        var tname = $("#t_nameadd").val();
        var tpwd = $("#t_pwdadd").val();
        var sex = $("#sexadd").val();
        var address = $("#addressadd").val();
        var age = $("#ageadd").val();

       console.log(tno,sex,address+"fuck..............");
        $.ajax({
            url:"<%=basePath%>admin/addTeacher",
            type:"post",
            data:{"tno":tno,"tname":tname,"tpwd":tpwd,"sex":sex,"address":address,"age":age},
            success:function (data) {
                console.log(data);
                if(data.message=="新增成功"){
                    window.location.reload();//刷新页面
                    $("#windowAdd").hide();//将添加框隐藏起来
                }else{
                    alert("新增失败")
                }

            },error:function () {
                alert("服务器出错!")
            }

        })
    }

    //删除操作
    function delTeacher(obj){
        var tid = $(obj).parent().parent().find("td").eq(0).text()
        console.log("tno="+tno)
        var isNo = confirm("是否确认删除该教师")
        if (isNo){
            $.ajax({
                url:"<%=basePath%>admin/deleteTeacherById",
                type:"post",
                data:{"tno":tno},

                success:function (data) {
                    console.log(data)
                    if(date>0){
                        window.location.reload();
                        alert("删除成功!!");
                    }else{
                        alert("删除失败");
                    }

                },
                error:function(){
                    alert("服务器出错！");
                }
            })
        }
    }

    //打开修改页面
    function updTeacherWindow(obj) {
        //显示修改的模态框
        $("#windowUpd").show();
        //获取当前选中行的所有数据
        var tno = $(obj).parent().parent().find("td").eq(0).text()
        var tname = $(obj).parent().parent().find("td").eq(1).text()
        var tpwd = $(obj).parent().parent().find("td").eq(2).text()
        var sex = $(obj).parent().parent().find("td").eq(3).text()
        var address = $(obj).parent().parent().find("td").eq(4).text()
        var age = $(obj).parent().parent().find("td").eq(5).text()

        //把选中行的数据复制给修改框里的表单元素
        $("#t_noUpd").val(tno)
        $("#t_nameUpd").val(tname)
        $("#t_pwdUpd").val(tpwd)
        $("#sexUpd").val(sex)
        $("#addressUpd").val(address)
        $("#ageUpd").val(age)

    }

    //关闭修改界面
    function closeUpdWindow() {
        $("#windowUpd").hide()
    }

    //修改教师信息
    function updTeacher(){
        //获取文本框的值
        var tno =  $("#t_noUpd").val()
        var tname = $("#t_nameUpd").val()
        var tpwd = $("#t_pwdUpd").val()
        var sex = $("#sexUpd").val()
        var address = $("#addressUpd").val()
        var age =  $("#ageUpd").val()

        $.ajax({
            url:"<%=basePath%>admin/teacherInformationUpdate",
            type:"post",
            data:{"tno":tno,"tname":tname,"tpwd":tpwd,"sex":sex,"address":address,"age":age},
            success:function (data) {
                console.log(data);
                if(data>0){
                    alert("修改成功");
                }else{
                    alert("修改失败")
                }
               /* select();*/
                $("#windowUpd").hide();
            }
        })
    }

</script>
</html>
